package com.xckj.ibox.ui.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.xckj.ibox.R;
import com.xckj.ibox.bean.BannerBean;
import com.xckj.ibox.bean.UserInfoBean;
import com.xckj.ibox.databinding.FragmentMyBinding;
import com.xckj.ibox.event.LoginStateEvent;
import com.xckj.ibox.framework.BaseFragment;
import com.xckj.ibox.global.Constant;
import com.xckj.ibox.iview.IMyView;
import com.xckj.ibox.presenter.MyPresenter;
import com.xckj.ibox.ui.activity.BrowserActivity;
import com.xckj.ibox.ui.activity.LoginActivity;
import com.xckj.ibox.utils.AndroidUtil;
import com.xckj.ibox.utils.LoadImageUtil;
import com.xckj.ibox.utils.SpUtil;
import com.xckj.ibox.view.CustomButtonDialogView;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.DrawableIndicator;
import com.youth.banner.listener.OnBannerListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.Objects;

/**
 * @Author: ChenYao
 * @Describe: 我的
 * @Date: 2023/6/14 16:58
 */
public class MyFragment extends BaseFragment<FragmentMyBinding> implements IMyView {
    private boolean isVisible = true;
    private String mobilePhone;

    @Override
    protected boolean isStatusBarDarkFont() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initData() {
        setListener();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isHidden()) {
            MyPresenter myPresenter = new MyPresenter(this);
            String appToken = SpUtil.getString(requireActivity(), Constant.APP_TOKEN);
            if (!TextUtils.isEmpty(appToken)) {
                myPresenter.getUserInfo(appToken);
            }
        }
    }

    private void setListener() {
        dataBinding.ivAvatar.setOnClickListener(view -> {
            if (AndroidUtil.isFastDoubleClick()) {
                return;
            }
            BrowserActivity.start(requireActivity(), Constant.H5Address.USER_INFO);
        });
        dataBinding.ivMessage.setOnClickListener(view -> {
            if (AndroidUtil.isFastDoubleClick()) {
                return;
            }
            BrowserActivity.start(requireActivity(), Constant.H5Address.MESSAGE_URL);
        });
        dataBinding.ivSetting.setOnClickListener(view -> {
            if (AndroidUtil.isFastDoubleClick()) {
                return;
            }
            BrowserActivity.start(requireActivity(), Constant.H5Address.SETTING_URL);
        });
        dataBinding.ivState.setOnClickListener(view -> {
            if (AndroidUtil.isFastDoubleClick()) {
                return;
            }
            isPhoneVisible();
        });
        dataBinding.ivCollectOrder.setOnClickListener(view -> {
            if (AndroidUtil.isFastDoubleClick()) {
                return;
            }
            BrowserActivity.start(requireActivity(), Constant.H5Address.COLLECT_ORDER_URL);
        });
        dataBinding.ivShoppingMallOrder.setOnClickListener(view -> {
            if (AndroidUtil.isFastDoubleClick()) {
                return;
            }
            BrowserActivity.start(requireActivity(), Constant.H5Address.SHOPPING_MALL_ORDER_URL);
        });
        dataBinding.llCollection.setOnClickListener(view -> {
            if (AndroidUtil.isFastDoubleClick()) {
                return;
            }
            BrowserActivity.start(requireActivity(), Constant.H5Address.COLLECTION_URL);
        });
        dataBinding.llMyWallet.setOnClickListener(view -> {
            if (AndroidUtil.isFastDoubleClick()) {
                return;
            }
            BrowserActivity.start(requireActivity(), Constant.H5Address.MY_WALLET_URL);
        });
        dataBinding.llMyPoints.setOnClickListener(view -> {
            if (AndroidUtil.isFastDoubleClick()) {
                return;
            }
            BrowserActivity.start(requireActivity(), Constant.H5Address.MY_POINTS_URL);
        });
        dataBinding.llMyGoldenBean.setOnClickListener(view -> {
            if (AndroidUtil.isFastDoubleClick()) {
                return;
            }
            BrowserActivity.start(requireActivity(), Constant.H5Address.MY_GOLDEN_BEAN_URL);
        });
        dataBinding.llInvite.setOnClickListener(view -> {
            if (AndroidUtil.isFastDoubleClick()) {
                return;
            }
            BrowserActivity.start(requireActivity(), Constant.H5Address.INVITE_URL);
        });
    }

    private void isPhoneVisible() {
        if (isVisible) {
            dataBinding.ivState.setImageResource(R.mipmap.icon_close_state);
            dataBinding.tvPhoneNumber.setText(AndroidUtil.mobileEncrypt(mobilePhone));
            isVisible = false;
            SpUtil.put(requireContext(), Constant.IS_VISIBLE, true);
        } else {
            dataBinding.ivState.setImageResource(R.mipmap.icon_open_state);
            dataBinding.tvPhoneNumber.setText(mobilePhone);
            isVisible = true;
            SpUtil.put(requireContext(), Constant.IS_VISIBLE, false);
        }
    }


    @Override
    public void getUserInfoSuccess(UserInfoBean userInfoBean) {
        setUserInfo(userInfoBean);
    }

    private void setUserInfo(UserInfoBean userInfoBean) {
        mobilePhone = userInfoBean.phone;
        LoadImageUtil.loadAvatar(userInfoBean.head_image, dataBinding.ivAvatar);
        if (SpUtil.getBoolean(requireActivity(), Constant.IS_VISIBLE)) {
            dataBinding.ivState.setImageResource(R.mipmap.icon_close_state);
            dataBinding.tvPhoneNumber.setText(AndroidUtil.mobileEncrypt(mobilePhone));
        } else {
            dataBinding.ivState.setImageResource(R.mipmap.icon_open_state);
            dataBinding.tvPhoneNumber.setText(mobilePhone);
        }
        dataBinding.tvUserName.setText(userInfoBean.nick_name);
    }

    @Override
    public void tokenLose() {
        new CustomButtonDialogView(requireActivity()).setTitle("温馨提示").setMessage("当前登录状态已失效,请重新登录").showOneButton(true).setSureText("确定").setCancelable(true).setSureOnClickListener(view -> {
            SpUtil.remove(requireActivity(), Constant.APP_TOKEN);
            SpUtil.remove(requireActivity(), Constant.TOKEN);
            SpUtil.remove(requireActivity(), Constant.IS_AUTO_LOGIN);
            startActivity(new Intent(requireActivity(), LoginActivity.class));
            requireActivity().finish();
        }).build().show();
    }

    @Override
    public void failed(String message) {
        showMsg(message);
    }

}
